# -*- coding:utf-8 -*-
import datetime
import requests
import pymysql
import time, re, os
from lxml import etree
from selenium import webdriver
from bs4 import BeautifulSoup
import sys


db1 = pymysql.connect(host='112.125.25.114', port=3306, user="python", password="pythonselect", database="mykhadmin")
cursor1 = db1.cursor()


def login_cpquery(login_inst):
    """登录中国及多国"""
    profile = webdriver.FirefoxOptions()
    profile.set_preference('network.proxy.type', 1)
    browser = webdriver.Firefox(options=profile, executable_path=r'D:\工具\geckodriver-v0.15.0-win64\geckodriver.exe')
    browser.get("http://cpquery.cnipa.gov.cn/")
    time.sleep(15)

    if login_inst == 'XH':
        browser.find_element_by_id('username').clear()
        browser.find_element_by_id('username').send_keys('CN00675769')
        time.sleep(1)
        browser.find_element_by_id('password').clear()
        browser.find_element_by_id('password').send_keys('Gv89NWWY@')
    elif login_inst == 'XHHG':
        browser.find_element_by_id('username').clear()
        browser.find_element_by_id('username').send_keys('42264')
        time.sleep(1)
        browser.find_element_by_id('password').clear()
        browser.find_element_by_id('password').send_keys('Xhhg0613@@')
    elif login_inst == 'WB':
        # 15978554102 Hhr31415926!
        # 17362997328 Ccy1994!
        # 15090891351 Ccy1994!
        # 13296529358 king123!B
        browser.find_element_by_id('username1').clear()
        browser.find_element_by_id('username1').send_keys('13296529358')
        time.sleep(1)
        browser.find_element_by_id('password1').clear()
        browser.find_element_by_id('password1').send_keys('king123!B')
    else:
        print('请手动输入密码')
        time.sleep(35)
    time.sleep(5)
    is_search_ok(browser)
    current_url = browser.current_url
    return browser, current_url


def connect():
    sql = "select application_number from external_orders"  # 获取申请号
    cursor1.execute(sql)
    content = cursor1.fetchall()
    sqh_li = []
    for sqh in content:
        application = sqh[0].replace('ZL', '')
        sqh_li.append(application)
    return sqh_li


def is_search_ok(browser):
    page_source = browser.page_source
    html = etree.HTML(page_source)
    chrc = ''.join(html.xpath('//p/text()'))
    if '费用信息' in chrc:
        print('查询页面登录成功')
    else:
        print('查询页面没登录进去')
        time.sleep(3)
        is_search_ok(browser)


def parse_impower_day(browser, current_url, sqh):
    pass


def get_msg(sqh, browser, current_url, start_time):
    pass


def StartParse(login_inst):
    """主函数体"""
    browser, current_url = login_cpquery(login_inst)    # 登录中国及多国
    start_time = datetime.datetime.now()
    if login_inst == 'WB':
        content = connect()     # 获取到申请号列表
        for i in range(0, len(content)):
            print('当前正在获取第{}个专利的授权日,共{}个！'.format(i + 1, len(content)))
            sqh = content[i]
            get_msg(sqh, browser, current_url, start_time)


if __name__ == '__main__':
    # 1. 协和端口账号查询状态 第一遍查询插入  账号: CN00675769  密码: Gv89NWWY@  查询: 2020207090033
    # StartParse('XH')
    # 2. 信合红谷端口账号查询状态 第二遍查询更新  账号: 42264  密码: Xhhg0613@@  查询: 2019214697762
    # StartParse('XHHG')
    # 3 第三遍使用公众登录查询 查询: 2020207090033
    StartParse('WB')
    # 4. 本地 xhhg_patnet_status	导入线上库，已经更新了优先审查信息
    # update_system_status()

